package com.hefeng.unit.controller;

import com.alibaba.fastjson2.JSONObject;
import com.hefeng.common.core.config.BaseStaticParameter;
import com.hefeng.common.core.utils.EmptyUtils;
import com.hefeng.common.core.utils.HttpRequester;
import com.hefeng.common.core.utils.HttpRespons;
import com.hefeng.common.core.utils.file.FileUtils;
import com.hefeng.common.core.utils.oss.AliOssUtil;
import com.hefeng.common.core.web.domain.AjaxResult;
import com.hefeng.unit.controller.entity.ParamEntity;
import com.hefeng.unit.domain.GyUser;
import com.hefeng.unit.service.GyUserService;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/unit/index")
public class LoginController {
    @Autowired
    private GyUserService gyUserService;
    private static HttpRequester requester = new HttpRequester();

    @RequestMapping(value = "/login", produces = "application/json;charset=UTF-8")
    @ApiOperation("工业用户登录")
    public AjaxResult login(@RequestBody JSONObject userLogin) {
        String UNITAPPID = "wx217390826b4407ae";
        String UNITSECRET = "55d7c7eb4109107bdf055834cb7ee0a5";
        log.info("sssssssssssssssss:"+BaseStaticParameter.UNITAPPID);
        Map<String, Object> data = new HashMap<String, Object>();
        JSONObject obj = new JSONObject();
        //微信小程序验证
        log.info("微信用户登录");
        String code = userLogin.getString("code");
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Map<String, String> map = new HashMap<String, String>();
        //参数map
        map.put("appid", UNITAPPID);
        map.put("secret", UNITSECRET);
        map.put("code", code);
        //微信那边的接口，grant_type=authorization_code是固定的
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + UNITAPPID +
                "&secret=" + UNITSECRET + "&js_code=" + code + "&grant_type=authorization_code";
        try {
            //微信后台验证
            HttpRespons ret = requester.sendPost(url, map);
            obj = JSONObject.parseObject(ret.getContent());
            String openid = obj.getString("openid");
            //获取用户信息
            GyUser user = gyUserService.getWxuserById(openid);
            if (EmptyUtils.isNotEmpty(user)) {
                data.put("user", user);
                return AjaxResult.success("登录成功！", data);
            }
            return AjaxResult.success(obj);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("500", "登录超时，请退出后重新登录！");
        }
    }

    /**
     * 方法说明: 上传图片
     *
     * @Param: []
     * @return: com.hefeng.common.core.web.domain.AjaxResult
     * @Author: fwb
     */
    @ApiOperation("上传图片接口")
    @ResponseBody
    @RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public AjaxResult uploadPhoto(@RequestParam(value = "file") MultipartFile[] files) {

        List<Map> list = new ArrayList<>();
        try {
            if (EmptyUtils.isEmpty(files)) {
                System.out.println("文件为空！");
                return AjaxResult.error("文件不能为空", false);
            }

            for (MultipartFile file : files) {
                Map map = new HashMap<>();
                String fileName = file.getOriginalFilename();  // 文件名
                String filePath = "";
                if (FileUtils.isImageFile(fileName.substring(fileName.indexOf("."), fileName.length()))) {
                    filePath = "pic";
                }
                if (fileName.substring(fileName.indexOf("."), fileName.length()).equals(".mp4")) {
                    filePath = "video";
                }
                if (fileName.substring(fileName.indexOf("."), fileName.length()).equals(".mp3")) {
                    filePath = "MP3";
                }
                //创建输出文件对象
                String fname = UUID.randomUUID().toString() + FileUtils.getName(fileName);
                AliOssUtil ossUtil = new AliOssUtil();
                String url = ossUtil.uploadFile(file.getInputStream(), "gas/user/" + filePath + "/" + fname);
                map.put("url", url);
                list.add(map);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.success("上传成功", list);
    }

    /**
     * 方法说明: 获取单位信息列表
     *
     * @Param: []
     * @return: com.hefeng.common.core.web.domain.AjaxResult
     * @Author: fwb
     */
    @RequestMapping(value = "/getUnitList", produces = "application/json;charset=UTF-8")
    @ApiOperation("获取单位信息列表")
    public AjaxResult getUnitList(@RequestBody ParamEntity paramEntity) {

        return AjaxResult.success();
    }
}
